import Vue from 'vue'
import Vuex from 'vuex'

import { parseJsonStr, stringifyObj, findChildItem, getPermissions } from '@utility/utility'

import user from './modules/user.js'
import publicCode from './modules/publicCode.js'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    activeRouter: '',
    cachedPages: [],
    permissions: null,
    userPermissions: null,
    isTestEnv: process.env.VUE_APP_BUILD_ENV === 'test',
    leftMenuPcode: null,
  },
  getters: {
    cachedPages: state => state.cachedPages,
    permissions: state => {
      const permissions = state.permissions ?? parseJsonStr(localStorage.getItem('ENTERPRISE_PERMISSION_CODES'))
      if (permissions && permissions.length) {
        return permissions
      }
      return null
    },
    leftNavMenus: state => {
      const userPermissions = state.userPermissions ?? parseJsonStr(localStorage.getItem('ENTERPRISE_PERMISSIONS'))
      if (userPermissions) {
        if (state.leftMenuPcode) {
          const menu =
            findChildItem(userPermissions, state.leftMenuPcode, { fieldId: 'belongPage', fieldChild: 'menuVOS' }) || {}
          const { node } = menu
          if (node) {
            return node.menuVOS
          }
        }
      }
      return userPermissions
    },
    isTestEnv: state => state.isTestEnv,
  },
  actions: {
    getUserPermissions(context) {
      const menuList = [
        {
          belongPage: 'ENTERPRISE_LIST',
          menuCode: '',
          menuName: '企业名录',
          menuImagePath: 'iconfont icon-minglu',
          menuVOS: null,
        },
        {
          belongPage: 'JUDGRULE_LIST',
          menuCode: '',
          menuName: '判定规则管理',
          menuImagePath: 'iconfont icon-taizhang',
          menuVOS: null,
        },
        {
          belongPage: 'JUDGALGORITHM_LIST',
          menuCode: '',
          menuName: '判定算法管理',
          menuImagePath: 'iconfont icon-fenxi',
          menuVOS: null,
        },
        {
          belongPage: 'JUDGMONITOR_LIST',
          menuCode: '',
          menuName: '企业判定监测',
          menuImagePath: 'iconfont icon-a-23guize',
          menuVOS: null,
        },
        {
          belongPage: 'JUDGAPPROVAL_LIST',
          menuCode: '',
          menuName: '企业判定审批',
          menuImagePath: 'iconfont icon-shenpi',
          menuVOS: null,
        },
        {
          belongPage: 'ENTERPRISE_LIST_UPDATE',
          menuCode: '',
          menuName: '企业名录维护',
          menuImagePath: 'iconfont icon-xiugai',
          menuVOS: null,
        },
        {
          belongPage: 'HELPPOLICY_LIST',
          menuCode: '',
          menuName: '帮扶政策',
          menuImagePath: 'iconfont icon-zhengce',
          menuVOS: null,
        },
        {
          belongPage: 'OPTIMIZATIONPLAN_LIST',
          menuCode: '',
          menuName: '帮扶政策优化方案',
          menuImagePath: 'iconfont icon-jisuanjitiyan',
          menuVOS: null,
        },
      ]
      // 管理员才有账户管理的菜单
      /*  if (context.getters['user/getUserInfo'].permissions.includes('accout_management')) {
        menuList.push({
          belongPage: 'ACCOUNT_MANAGE',
          menuCode: '',
          menuName: '账户管理',
          menuImagePath: 'iconfont icon-paiming',
          menuVOS: null,
        })
      } */
      context.commit('setUserPermissions', menuList)
      context.commit('setPermissions', { list: menuList })
      /* if (context.getters.userInfo) {
        const { status = 0, data = [] } = await getUserpermissions(context.getters.userInfo.userId)
        if (status === 200 && data.length) {
          context.commit('setUserPermissions', data)
          context.commit('setPermissions', { list: data })
        } else {
        }
      } */
    },
  },
  mutations: {
    setActiveRouter(state, router) {
      state.activeRouter = router
    },
    setCachedPages(state, val) {
      state.cachedPages = val
    },
    clearCommonAll(state) {
      state.activeRouter = null
      state.cachedPages = []
      state.orgId = null
      state.permissions = null
    },
    // TODO 合并 多余的 permission
    setUserPermissions(state, val) {
      state.userPermissions = val
      localStorage.setItem('ENTERPRISE_PERMISSIONS', stringifyObj(val))
    },
    setLeftMenuPcode(state, pcode) {
      state.leftMenuPcode = pcode
    },
    setPermissions(state, { list = [] }) {
      let permissions = []
      if (list.length) {
        permissions = getPermissions(list, permissions)
      }
      localStorage.setItem('ENTERPRISE__PERMISSION_CODES', stringifyObj(permissions))
      state.permissions = permissions
    },
  },
  modules: { user, publicCode },
})
